#########################################################################################
# 
# Create Variables
#
#########################################################################################

#########################################################################################
### Democratic Republic of Congo

# Demographics
table(congo$female)
table(congo$age)
table(congo$edu_level) 
table(congo$hh_size)

congo$edu[congo$edu_level==0 | congo$edu_level==1 | congo$edu_level==2] <- "a_low"
congo$edu[congo$edu_level==3] <- "b_medium"
congo$edu[congo$edu_level>=4] <- "c_high"

congo$inc[congo$assets<=quantile(congo$assets, .33)] <- "a_low"
congo$inc[congo$assets>quantile(congo$assets, .33) & congo$assets<=quantile(congo$assets, .66)] <- "b_medium"
congo$inc[congo$assets>quantile(congo$assets, .66)] <- "c_high"

# Standardize
congo$age.z <- (congo$age - mean(congo$age, na.rm=T))/(2*sd(congo$age, na.rm=T))
congo$edu.z <- (congo$edu_level - mean(congo$edu_level, na.rm=T))/(2*sd(congo$edu_level, na.rm=T))
congo$income.z <- (congo$assets - mean(congo$assets, na.rm=T))/(2*sd(congo$assets, na.rm=T))
congo$hh_size.z <- (congo$hh_size - mean(congo$hh_size, na.rm=T))/(2*sd(congo$hh_size, na.rm=T))

# Other Violence
table(congo$murder_yes)
table(congo$leavehome_yes)

# Outcome: Civic Engagement
congo$soc_part <- congo$wom_member +
  congo$farmer_member +
  congo$pray_member +
  congo$health_member + 
  congo$edu_member +
  congo$save_member +
  congo$water_member +
  congo$NGO_member 

congo$soc_part.2 <- ifelse(congo$soc_part>=1, 1, 0)

table(congo$soc_part.2)

# For robustness as requested by reviewer:
congo$soc_part.rob <- ifelse(congo$wom_member==1 | congo$pray_member==1, 1, 0)

# Outcome: Interethnic Relations
congo$interethnic <- (congo$interethnic_rel1_rec +
                        congo$interethnic_rel2_rec + 
                        congo$interethnic_rel3_rec + 
                        congo$interethnic_rel4_rec + 
                        congo$interethnic_rel5_rec + 
                        congo$interethnic_rel6_rec + 
                        congo$interethnic_rel7_rec + 
                        congo$interethnic_rel8_rec)/8 

congo$interethnic <- (congo$interethnic-mean(congo$interethnic, na.rm=T))/sd(congo$interethnic, na.rm=T)

# Outcome: Donation
congo$donate <- ifelse(congo$donate7>0, 1, 0)

# Outcome: Post-Traumatic Growth
congo$PTG <- congo$ptg1 + congo$ptg2 + congo$ptg3 + congo$ptg4 + congo$ptg5 + congo$ptg6 +
  congo$ptg7 + congo$ptg8 + congo$ptg9 + congo$ptg10 

congo$PTG <- congo$PTG/10

#########################################################################################
### Liberia

# Demographics
table(liberia$r_female)
table(liberia$r_age)
table(liberia$edu_level)  

liberia$female <- liberia$r_female
liberia$edu_level<- as.numeric(liberia$edu_level)

liberia$edu[liberia$edu_level==0 | liberia$edu_level==1 | liberia$edu_level==2] <- "a_low"
liberia$edu[liberia$edu_level==3 ] <- "b_medium"
liberia$edu[liberia$edu_level>=4] <- "c_high"

liberia$inc[liberia$impact_income<=quantile(liberia$impact_income, .33, na.rm=T)] <- "a_low"
liberia$inc[liberia$impact_income>quantile(liberia$impact_income, .33, na.rm=T) & liberia$impact_income<=quantile(liberia$impact_income, .66, na.rm=T)] <- "b_medium"
liberia$inc[liberia$impact_income>quantile(liberia$impact_income, .66, na.rm=T)] <- "c_high"

# Standardize
liberia$age.z <- (liberia$r_age - mean(liberia$r_age, na.rm=T))/(2*sd(liberia$r_age, na.rm=T))
liberia$edu.z <- (liberia$edu_level - mean(liberia$edu_level, na.rm=T))/(2*sd(liberia$edu_level, na.rm=T))
liberia$income.z <- (liberia$impact_income - mean(liberia$impact_income, na.rm=T))/(2*sd(liberia$impact_income, na.rm=T))
liberia$hh_size.z <- (liberia$fam_size - mean(liberia$fam_size, na.rm=T))/(2*sd(liberia$fam_size, na.rm=T))

# Region
liberia$county.1 <-ifelse(liberia$county=="Maryland", 1, 0)
liberia$county.2 <-ifelse(liberia$county=="River Gee", 1, 0)

# Other Violence
table(liberia$cw_kill) 
table(liberia$cw_displaced)

# Outcome: Civic Engagement
table(liberia$outcome_ca)

# Outcome: Interethnic Relations
table(liberia$trust1)

# Outcome: Donation
table(liberia$trust4)


#########################################################################################
### Sri Lanka

# Demographics
sri$age <- sri$B1a
sri$female <- ifelse(sri$B1b==2, 1, 0)
sri$edu_level <- sri$B3a 

sri$educ[sri$edu_level==0 |sri$edu_level==1] <- "a_low"
sri$educ[sri$edu_level==2 |sri$edu_level==3] <- "b_medium"
sri$educ[sri$edu_level>=4] <- "c_high"

sri$inc[sri$A5<=quantile(sri$A5, .33, na.rm=T)] <- "a_low"
sri$inc[sri$A5>quantile(sri$A5, .33, na.rm=T) & sri$A5<=quantile(sri$A5, .66, na.rm=T)] <- "b_medium"
sri$inc[sri$A5>quantile(sri$A5, .66, na.rm=T)] <- "c_high"

sri$A1_1 <- ifelse(is.na(sri$A1_1)==F, 1, 0)
sri$A1_2 <- ifelse(is.na(sri$A1_2)==F, 1, 0)
sri$A1_3 <- ifelse(is.na(sri$A1_3)==F, 1, 0)
sri$A1_4 <- ifelse(is.na(sri$A1_4)==F, 1, 0)
sri$A1_5 <- ifelse(is.na(sri$A1_5)==F, 1, 0)
sri$A1_6 <- ifelse(is.na(sri$A1_6)==F, 1, 0)
sri$A1_7 <- ifelse(is.na(sri$A1_7)==F, 1, 0)
sri$A1_8 <- ifelse(is.na(sri$A1_8)==F, 1, 0)
sri$A1_9 <- ifelse(is.na(sri$A1_9)==F, 1, 0)
sri$A1_10 <- ifelse(is.na(sri$A1_10)==F, 1, 0)

sri$hh_size <- sri$A1_1 + sri$A1_2 + sri$A1_3 + sri$A1_4 + sri$A1_5  + sri$A1_6 + sri$A1_7 + sri$A1_8 + sri$A1_9 + sri$A1_10

# Standardize
sri$age.z <- (sri$age - mean(sri$age, na.rm=T))/(2*sd(sri$age, na.rm=T))
sri$edu.z <- (sri$edu_level - mean(sri$edu_level, na.rm=T))/(2*sd(sri$edu_level, na.rm=T))
sri$income.z <- (sri$A5- mean(sri$A5, na.rm=T))/(2*sd(sri$A5, na.rm=T))
sri$hh_size.z <- (sri$hh_size - mean(sri$hh_size, na.rm=T)) / (2*sd(sri$hh_size, na.rm=T))

# Region
sri$prov.2 <- ifelse(sri$Province==2, 1, 0)
sri$prov.3 <- ifelse(sri$Province==3, 1, 0)
sri$prov.4 <- ifelse(sri$Province==4, 1, 0)
sri$prov.5 <- ifelse(sri$Province==5, 1, 0)
sri$prov.6 <- ifelse(sri$Province==6, 1, 0)
sri$prov.7 <- ifelse(sri$Province==7, 1, 0)
sri$prov.8 <- ifelse(sri$Province==8, 1, 0)
sri$prov.9 <- ifelse(sri$Province==9, 1, 0)

sri$eastern <- ifelse(sri$Province==8, 1, 0)

# Other Violence
sri$trauma <- ifelse(sri$D3a==1, 1, 0) 
sri$killed <- ifelse(sri$D3c==1, 1, 0) 
sri$displace <- ifelse(sri$D7==1, 1, 0) 

# Pre-Exposure Civic Engagment
sri$prior <- ifelse(sri$H3e==2, 0, 1)
sri$prior.2 <- ifelse(is.na(sri$H3e)==T, 0, sri$prior)
sri$prior.3 <- sri$prior
sri$prior.3[sri$age<=33 & sri$prior==0] <- NA 
  
# Outcome: Civic Engagement
sri$active_a <- ifelse(sri$C21a==1, 1, 0)
sri$active_b <- ifelse(sri$C21b==1, 1, 0)
sri$active_c <- ifelse(sri$C21c==1, 1, 0)
sri$active_d <- ifelse(sri$C21d==1, 1, 0)
sri$active_e <- ifelse(sri$C21e==1, 1, 0)
sri$active_f <- ifelse(sri$C21f==1, 1, 0)
sri$active_g <- ifelse(sri$C21g==1, 1, 0)
sri$active_h <- ifelse(sri$C21h==1, 1, 0)
sri$active_i <- ifelse(sri$C21i==1, 1, 0)
sri$active_j <- ifelse(sri$C21j==1, 1, 0)

sri$soc_part <- sri$active_a + sri$active_b + sri$active_c +
  sri$active_d + sri$active_e + sri$active_f +
  sri$active_g + sri$active_h + sri$active_i +
  sri$active_i

sri$soc_part.2 <- ifelse(sri$soc_part>=1, 1, 0)

# For robustness as requested by reviewer:
sri$soc_part.rob <- ifelse(sri$active_e==1 | sri$active_f==1, 1, 0)

# Outcome: Interethnic Relations
sri$outgrouptrust <- NA
sri$outgrouptrust[sri$B7==1] <- (sri$C18b[sri$B7==1] + sri$C18c[sri$B7==1] + sri$C18d[sri$B7==1])/3
sri$outgrouptrust[sri$B7==2] <- (sri$C18a[sri$B7==2] + sri$C18c[sri$B7==2] + sri$C18d[sri$B7==2])/3
sri$outgrouptrust[sri$B7==3] <- (sri$C18a[sri$B7==3] + sri$C18b[sri$B7==3] + sri$C18d[sri$B7==3])/3
sri$outgrouptrust[sri$B7==4] <- (sri$C18a[sri$B7==4] + sri$C18b[sri$B7==4] + sri$C18c[sri$B7==4])/3

sri$outgrouptrust <- (sri$outgrouptrust - mean(sri$outgrouptrust, na.rm=T))/sd(sri$outgrouptrust, na.rm=T)

# Outcome: Post-traumatic Growth
sri$E2a <- ifelse(sri$E2a==88, NA, sri$E2a)
sri$E2b <- ifelse(sri$E2b==88, NA, sri$E2b)
sri$E2c <- ifelse(sri$E2c==88, NA, sri$E2c)
sri$E2d <- ifelse(sri$E2d==88, NA, sri$E2d)
sri$E2e <- ifelse(sri$E2e==88, NA, sri$E2e)
sri$E2f <- ifelse(sri$E2f==88, NA, sri$E2f)
sri$E2g <- ifelse(sri$E2g==88, NA, sri$E2g)
sri$E2h <- ifelse(sri$E2h==88, NA, sri$E2h)
sri$E2i <- ifelse(sri$E2i==88, NA, sri$E2i)
sri$E2j <- ifelse(sri$E2j==88, NA, sri$E2j)

sri$PTG <- sri$E2a + sri$E2b + sri$E2c + sri$E2d + sri$E2e + sri$E2f +
  sri$E2g + sri$E2h + sri$E2i + sri$E2j 

sri$PTG <- sri$PTG/10

#########################################################################################
